package com.yanqu.road.server.cmd.activity.dafuweng.cross;

import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;

@Cmd(code = GameProtocol.S_DA_FU_WENG_CROSS_UNION_SYNC_ZHUFU,desc = "")
public class DfwZhufuSyncCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        MonopolyProto.SyncZhufuMsg syncZhufuMsg = MonopolyProto.SyncZhufuMsg.parseFrom(packet.getBytes());
        int activityId = syncZhufuMsg.getActivityId();
        String unionId = syncZhufuMsg.getUnionId();
        MonopolyProto.UnionDataMsg unionMsg = syncZhufuMsg.getUnionMsg();
        MonopolyProto.SyncZhuFuRespMsg zhufuMsg = syncZhufuMsg.getZhufuMsg();
        DaFuWengMgr.updateUnionMsg(activityId, unionId, unionMsg);
        for (CrossUnionMember member : new ArrayList<>(CrossUnionMgr.getUnionMemberMap(unionId).values())) {
            if (member.getServerId() == GameServer.getInstance().getServerId()) {
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                if (player != null && packet.getPlayerId() != member.getUserId()) {
                    player.sendPacket(ClientProtocol.U_DA_FU_WENG_SYNC_ZHU_FU, zhufuMsg.toBuilder());
                }
            }
        }
    }
}
